import router from "@/router"
import storage from "./storage"
import store from "@/store"
// 引入进度条
import nprogress from "nprogress"
import 'nprogress/nprogress.css'
router.beforeEach((to, from, next) => {
     // 打开进度条
     nprogress.start()
    let token = storage.get('qf-token')
//    console.log(token)
    //to 目标路由对象
    //from 源路由对象
    //next是个函数,调用之后,可以理解为放行
    // console.log(to)
    if (token) {
        // 判断是否有用户菜单
        if(!store.state.permission.userMenu.length){
            store.dispatch('permission/FETCH_MEMU_LIST')
            .then(()=>{
                next({path:to.path,replace: true,query:to.query});
            })
        }else{
            next()
        }
    } else {
        //没有token
        //1.访问的是登入页
        if (to.path === '/login') {
            next()
        } else {
            //2.访问的不是登入页 强行调回登入页
            next({ path: "/login" })
        }

    }
})
// 全局后置钩子 进入了路由组件之后再回调
router.afterEach((to,from)=>{
    nprogress.done()
    let matched=to.matched.filter((item,index)=>index>0)
    store.commit('permission/SET_CRUMB',matched)
})
export default router